package com.three.lab;


import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.three.lab.adapter.ViewPagerAdapter;
import com.three.lab.base.BaseActivity;
import com.three.lab.fragment.ClassifyFragment;
import com.three.lab.fragment.HomeFragment;
import com.three.lab.fragment.MeFragment;
import com.three.lab.fragment.MessageFragment;
import com.three.lab.fragment.ShopCarFragment;
import com.three.lab.fragment.WorkFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener{

    private ViewPager2 viewPager;
    private List<Fragment> fragments;
    private MenuItem menuItem;
    private BottomNavigationView bottomNavigationView;
    private HomeFragment homeFragment;
    //private WorkFragment workFragment;
    private ClassifyFragment classifyFragment;
    private ShopCarFragment shopCarFragment;
    private MessageFragment messageFragment;
    private MeFragment meFragment;
    private int curIndex = 0;

    @Override
    protected void initParms(Bundle bundle) {}

    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        setStatusBar(R.color.app_titlebar_cor);
        viewPager = $(R.id.view_pager);
        bottomNavigationView = $(R.id.bottom_navigation);
        viewPager.setUserInputEnabled(false);//禁止左右滑动
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    protected void setListener() {}

    @Override
    protected void widgetClick(View v) {}

    @Override
    protected void initData(Context mContext) {
        setupViewPager();
    }

    private void setupViewPager() {
        try {
            fragments = new ArrayList<>();
            if (homeFragment == null) {
                homeFragment = HomeFragment.newInstance(0,getString(R.string.home));
            }
            if (classifyFragment == null) {
                classifyFragment = ClassifyFragment.newInstance(1,getString(R.string.classify));
            }
            if (shopCarFragment == null) {
                shopCarFragment = ShopCarFragment.newInstance(2,getString(R.string.cart));
            }
            if (messageFragment == null) {
                messageFragment = MessageFragment.newInstance(3,getString(R.string.message));
            }

            if (meFragment == null) {
                meFragment = MeFragment.newInstance(4,getString(R.string.me));
            }
            fragments.add(homeFragment);
            fragments.add(classifyFragment);
            fragments.add(shopCarFragment);
            fragments.add(messageFragment);
            fragments.add(meFragment);
            ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
            viewPager.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_home:
               // setStatusBar(R.color.white);
                setViewPagerAtPosition(0);
                break;
            case R.id.item_classify:
               // setStatusBar(R.color.white);
                setViewPagerAtPosition(1);
                break;
            case R.id.item_cart:
               // setStatusBar(R.color.white);
                setViewPagerAtPosition(2);
                break;
            case R.id.item_message:
              //  setStatusBar(R.color.white);
                setViewPagerAtPosition(3);
                break;
            case R.id.item_me:
              //  setStatusBar(R.color.app_titlebar_cor);
                setViewPagerAtPosition(4);
                break;
        }
        return false;
    }

    private void setViewPagerAtPosition(int i) {
        try {
            curIndex = i;
            viewPager.setCurrentItem(i, true);//参数smoothScroll:是否平滑过渡(true:是,false:否)。
            setBottonMenu(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setBottonMenu(int i) {
        try {
            if (menuItem != null) {
                menuItem.setChecked(false);
            } else {
                bottomNavigationView.getMenu().getItem(0).setChecked(false);
            }
            menuItem = bottomNavigationView.getMenu().getItem(i);
            menuItem.setChecked(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private long lastDownTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long currentTime = System.currentTimeMillis();
            if (currentTime - lastDownTime > 2500) {
                lastDownTime = currentTime;
                showToast(getString(R.string.press_again_to_exit_the_app));
                return true;
            } else {
                finish();
                return false;
            }
        }
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}